package com.trkj.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trkj.wiki.mapper.DocMapper;
import com.trkj.wiki.pojo.Doc;
import com.trkj.wiki.pojo.DocExample;
import com.trkj.wiki.req.DocQueryReq;
import com.trkj.wiki.req.DocSaveReq;
import com.trkj.wiki.resp.DocQueryResp;
import com.trkj.wiki.resp.PageResp;
import com.trkj.wiki.util.CopyUtil;
import com.trkj.wiki.util.SnowFlake;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Log4j2
@Service
public class DocService {
    @Autowired
    private DocMapper docMapper;
    @Resource
    private SnowFlake snowFlake;

    public List<DocQueryResp> all (){
        DocExample docExample = new DocExample();
        docExample.setOrderByClause("sort asc");
        List<Doc> docList = docMapper.selectByExample(docExample);

        List<DocQueryResp> respList = CopyUtil.copyList(docList, DocQueryResp.class);
        return respList;
    }

    public PageResp<DocQueryResp> list(DocQueryReq req){
        DocExample docExample = new DocExample();
        DocExample.Criteria criteria = docExample.createCriteria();
        docExample.setOrderByClause("sort asc");
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Doc> docList = docMapper.selectByExample(docExample);
        PageInfo<Doc> pageInfo=new PageInfo<>(docList);
        log.info("总行数：{}",pageInfo.getTotal());
        log.info("总页数：{}",pageInfo.getPages());

    /*    List<DocResp> respList=new ArrayList<>();
        for (Doc doc : docList) {
            DocResp docResp = new DocResp();
            BeanUtils.copyProperties(doc,docResp);
            respList.add(docResp);
        }*/

        List<DocQueryResp> respList = CopyUtil.copyList(docList, DocQueryResp.class);
        PageResp<DocQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(respList);


        return pageResp;
    }

    /**
     * @Author Twx
     * @Date 2021/4/25 14:34
     * @Description 电子书新增/修改
     * @Param [req]
     * @Return void
     * @Since version-1.0
     */
    public void save(DocSaveReq req){
        Doc doc=CopyUtil.copy(req,Doc.class);
        if(ObjectUtils.isEmpty(req.getId())){
            //新增
            doc.setId(snowFlake.nextId());
            docMapper.insert(doc);
        }else {
            //更新
            docMapper.updateByPrimaryKey(doc);
        }

    }

    /*
     * @Author Twx
     * @Date 2021/4/25 20:28
     * @Description 删除电子书
     * @Param [id]
     * @Return void
     * @Since version-1.0
     */
    public void delete(long id){
        docMapper.deleteByPrimaryKey(id);
    }

    public void delete(List<String> ids){
        DocExample docExample = new DocExample();
        DocExample.Criteria criteria = docExample.createCriteria();
        criteria.andIdIn(ids);
        docMapper.deleteByExample(docExample);
    }

}
